#-*- coding:UTF-8 -*-
'''
Created on 2013-6-7

@author: hanjiewu
'''
#元类改变创建类的默认行为和创建方式
from time import ctime
from warnings import warn
print '*** Welcome to Metaclasses!'
print '\tMetaclass declaration first.'

class MetaC(type):
    def __init__(cls, name, bases, attrd):
        super(MetaC, cls).__init__(name, bases, attrd)
        print '*** Created class %r at :%s' % (name, ctime())

print '\tClass "Foo" declaration next.'
class Foo(object):
    __metaclass__ = MetaC
    def __init__(self):
        print '*** Instantiated class %r at : %s' % (self.__class__.__name__, ctime())

print '\tClass "Foo" instantiation next.'
f = Foo()
print '\tDONE'

class ReqStrSugRepr(type):
    def __init__(clz, name, bases, attrd):
        super(ReqStrSugRepr, clz).__init__(name, bases, attrd)
        if '__str__' not in attrd:
            raise TypeError('Class required overriding of __str__()')
        if '__repr__' not in attrd:
            warn("Class Suggests overriding of __repr__()\n", stacklevel = 3)
print '*** Defined ReqStrSugRepr (meta) class\n'

class Foo1(object):
    __metaclass__ = ReqStrSugRepr
    def __str__(self, *args, **kwargs):
        return 'Instance of class:' + self.__class__.__name__
    def __repr__(self, *args, **kwargs):
        return self.__class__.__name__
print '*** Defined Foo class\n'

class Bar(object):
    __metaclass__ = ReqStrSugRepr
    def __str__(self, *args, **kwargs):
        return 'Instance of class' + self.__class__.__name__
print '*** Defined Bar class\n'

class FooBar(object):
    __metaclass__ = ReqStrSugRepr
    
        